home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8849 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  52 lines

  1. Path: isonews.bbn.hp.com!hpbblb!news
  2. From: Matthias Dittrich <matti>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to make this get_time function work ?
  5. Date: 6 Mar 1996 17:31:04 GMT
  6. Organization: Hewlett-Packard Co.
  7. Message-ID: <4hki4o$1ju@hpbblb.bbn.hp.com>
  8. References: <4hhjct$3og@idefix.eunet.fi>
  9. NNTP-Posting-Host: trabant.bbn.hp.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/712)
  14. X-URL: news:4hhjct$3og@idefix.eunet.fi
  15.  
  16. mtg@neste.com (Michael Glasgow) wrote:
  17. >#include <stdio.h>
  18. >#include <ctype.h>
  19. >#include <time.h>
  20. >#include <math.h>
  21. >#include <string.h>
  22. >#define STRING_LENGTH 256
  23. >
  24. >int get_time() {
  25. If you are returning a pointer to char, then you should declare this:
  26. char* get_time()...
  27.  
  28. >        time_t t=time(NULL);
  29. >        struct tm *tp=localtime(&t);
  30. >        char time_str[STRING_LENGTH]={0}, str[STRING_LENGTH]={0};
  31. This is a critical point. With return str you are returning a pointer to
  32. the stack, which in most cases not contains what you are expecting. Declare
  33. str as static or return allocated memory for instance.
  34. Otherwise it's possible that your program writes a big file named core.
  35.  
  36. >        strftime(time_str, STRING_LENGTH, "%Y%m%d%H%M%S", tp);
  37. >        sprintf(str,"%s",time_str);
  38. >/*        printf("%s",str); */
  39. >    return str;
  40. >}
  41. >main() {
  42. >    int tim;
  43. This also must be: char* tim;
  44.  
  45. >    tim = get_time();
  46. >    printf("%s\n", tim);
  47. >}
  48. >
  49. Good luck,
  50. Matthias
  51.  
  52.